from typing import List

import jmespath

from config_params.base_util import baseUtil


class Assertions:

    def __init__(self, data):
        self.data = data

    def jme_path(self, exp):
        return jmespath.search(exp, self.data)

    def ResponseAssertion(self, class_name):
        assertion_dict = {}
        assertion_dict["name"] = self.jme_path("name") if self.jme_path("name") is not None else "Response Assertion"
        assertion_dict["comments"] = self.jme_path("comments") if self.jme_path("comments") is not None else ""
        assertion_dict["ignore_status"] = self.jme_path("ignore_status") if self.jme_path("ignore_status") is not None else False
        TestField = baseUtil.get_instance_attr("TestField")
        if self.jme_path("test_field") == "Assertion.response_data":
            assertion_dict["test_field"] = TestField.RESPONSE_BODY
        elif self.jme_path("test_field") == "Assertion.request_data":
            assertion_dict["test_field"] = TestField.REQUEST_DATA
        elif self.jme_path("test_field") == "Assertion.request_headers":
            assertion_dict["test_field"] = TestField.REQUEST_HEADERS
        elif self.jme_path("test_field") == "Assertion.sample_label":
            assertion_dict["test_field"] = TestField.URL
        elif self.jme_path("test_field") == "Assertion.response_data_as_document":
            assertion_dict["test_field"] = TestField.DOCUMENT
        elif self.jme_path("test_field") == "Assertion.response_headers":
            assertion_dict["test_field"] = TestField.RESPONSE_HEADERS
        elif self.jme_path("test_field") == "Assertion.response_code":
            assertion_dict["test_field"] = TestField.RESPONSE_CODE
        elif self.jme_path("test_field") == "Assertion.response_message":
            assertion_dict["test_field"] = TestField.RESPONSE_MESSAGE
        else:
            assertion_dict["test_field"] = TestField.RESPONSE_BODY
        TestType = baseUtil.get_instance_attr("TestType")
        if self.jme_path("test_type") == 16:
            assertion_dict["test_type"] = TestType.SUBSTRING
        elif self.jme_path("test_type") == 8:
            assertion_dict["test_type"] = TestType.EQUALS
        elif self.jme_path("test_type") == 1:
            assertion_dict["test_type"] = TestType.MATCHES
        elif self.jme_path("test_type") == 2:
            assertion_dict["test_type"] = TestType.CONTAINS
        else:
            assertion_dict["test_type"] = TestType.SUBSTRING
        assertion_dict["test_type_not"] = self.jme_path("test_type_not") if self.jme_path("test_type_not") is not None else False
        assertion_dict["test_type_or"] = self.jme_path("test_type_or") if self.jme_path("test_type_or") is not None else False
        assertion_dict["patterns"] = self.jme_path("patterns") if self.jme_path("patterns") is not None else []
        assertion_dict["custom_message"] = self.jme_path("custom_message") if self.jme_path("custom_message") is not None else ""
        Scope = baseUtil.get_instance_attr("Scope")
        if self.jme_path("scope") == "main":
            assertion_dict["scope"] = Scope.MAIN
        elif self.jme_path("scope") == "all":
            assertion_dict["scope"] = Scope.MAIN_AND_SUB
        elif self.jme_path("scope") == "children":
            assertion_dict["scope"] = Scope.SUB
        else:
            assertion_dict["scope"] = Scope.MAIN
        assertion_dict["is_enabled"] = self.jme_path("is_enabled") if self.jme_path("is_enabled") is not None else True
        http_sampler_obj = baseUtil.create_instance(class_name, assertion_dict)
        return http_sampler_obj

    def assertion(self):
        assertions_list = []
        if self.jme_path("type") == "ResponseAssertion":
            assertions_list.append(self.ResponseAssertion(self.jme_path("type")))
        return assertions_list
